local skill = fk.CreateSkill {
  name = "#ling__songzi_tu_skill",
  attached_equip = "ling__songzi_tu",
  tags = { Skill.Compulsory }
}

skill:addEffect(fk.AfterCardsMove, {
  global = true,
  priority = 0.001,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not table.contains(player.room.disabled_packs, "ling_cards") then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId, false).name == "ling__songzi_tu" then
              event:setCostData(self, { cid = info.cardId })
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local cid = event:getCostData(self).cid
    local room = player.room
    room:throwCard(cid, skill.name, player, player)
    if player.dead then return end
    player:drawCards(1, skill.name)
    if player.dead then return end
    math.randomseed(os.time())
    if math.random(10) == 1 and player:getMark(skill.name) == 0 then
      local skills = {}
      for g, general in pairs(Fk.generals) do
        if not table.contains(Fk:currentRoom().disabled_generals, g) and general.package.extensionName == "lingling" then
          for _, s in ipairs(general:getSkillNameList()) do
            if not player:hasSkill(s, true) and string.find(Fk:translate(":" .. s, "zh_CN"), "出牌阶段限一次") then
              table.insertIfNeed(skills, s)
            end
          end
        end
      end
      local gets = table.random(skills, 1)
      room:setPlayerMark(player, skill.name, 1)
      room:handleAddLoseSkills(player, gets[1])
    end
  end,
})

return skill
